/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mashart.pink.m.ws;

import java.util.Random;

/**
 *
 * @author peng
 */
public class FakeRestfulClient extends RestfulClient{
    // counter
    private int counter;

    // random number generator
    private Random rand = new Random();

    public FakeRestfulClient(){
        super();
        counter = 0;
    }

    /**
     * Send String to the server
     */
    public int push(String s){
        int res;
        // DEBUG
        System.out.print("[" + ( System.currentTimeMillis() % 1000000 ) + "]");
        System.out.println("sending: " + s);
        if( counter == 3 || counter == 4 || counter == 5 ){//
            // simulate: blocks the thread for 5 sec and then fails
            long tit = System.currentTimeMillis();
            while( System.currentTimeMillis() - tit < 10000 ){
            }
            res = -1;
            // DEBUG
            System.out.print("[" + ( System.currentTimeMillis() % 1000000 ) + "]");
            System.out.println("send failed: " + s);
        }else{
            // simulate: blocks the thread for 0.2 sec and then succeeds
            long tit = System.currentTimeMillis();
            while( System.currentTimeMillis() - tit < 200 ){
            }
//            super.push(s);
            res = 1;
            // DEBUG
            System.out.print("[" + ( System.currentTimeMillis() % 1000000 ) + "]");
            System.out.println("send succeeded: " + s);
        }
        counter ++;
        return res;
    }

    /**
     * Send an action String to the server
     */
    public int pushAction(String s){
        return push(s);
    }
}
